<?php
declare (strict_types = 1);
namespace app\course\logic\video;

use app\course\model\video\CourseSectionVideo;
use app\common\exception\LogicException;
use app\school\logic\school\School;
use app\course\model\base\Course;
use app\course\logic\base\CourseSection;
use app\course\event\subscribes\course\ExamineNotify;
use think\facade\Event;
use think\facade\Db;
use app\user\logic\user\Account;


/**
 * 前台接口逻辑层
 */
class VideoSection extends CourseSectionVideo
{

	public function getDataTxt($id)
	{
		return $this->where('id', $id)->value('data_txt');
	}

	public function addHour($course_id, $pid, $param)
	{
        // 验证数据
        $this->getValidate()->scene('add_hour')->failException()->check($param);

        // 课程设置的目录层级与当前层级一致，方为课时
        $course    = app(Course::class)->findOrFail($course_id);
        $sec_level = $course->section_level;

        if ($pid == 0) {
            if ($sec_level != 1) {
                throw new LogicException("课时层级错误");
            }
            $level = 1;
        } else {
            $data = app(CourseSection::class)->findOrFail($pid);
            if ($course_id != $data->course_id) {
                throw new LogicException("课程与课时不匹配");
            }
            if ($data->level + 1 != $sec_level) {
                throw new LogicException("课时层级错误");
            }
            $level = $data->level + 1;
        }

        // 添加课时data
        $add['data_type'] = $param['course_data_type'];
        $add['attach_id'] = $add['data_type'] == 3 ? 0 : ($param['course_data_attach_id']??0);
        $add['data_txt']  = $add['data_type'] == 3 ? ($param['course_data_txt']??'') : '';

		$hour_data = $this::create($add);
        if ( $hour_data->isEmpty() ) {
            throw new LogicException("新建课时失败");
        }

        // 参数
        $param['pid']       	 = $pid;
        $param['status']    	 = app(CourseSection::class)->getHourAuditStatus(request()->mid);
        $param['course_id'] 	 = $course_id;
        $param['course_type'] 	 = $course->course_type;
        $param['is_last']   	 = 1;
        $param['level'] 		 = $level;
        $param['course_data_id'] = $hour_data->id;
        $param['update_teacher'] = request()->mid;

        // 创建课时
        $create = CourseSection::create($param);
        if($create->isEmpty()){
            throw new LogicException("添加失败");
        }

        if ( $param['status'] != 1 ) { // 2机构审，3平台审
            Event::subscribe(ExamineNotify::class);
            Event::trigger('ExamineNotify', [
                'type'  => 'submit',   // 前端讲师提交，examine后台审核结果通知
                'data'  => [
                    'admin_user_id' => $param['status'] == 2 ? app(School::class)->getSchoolAdminUserId($course->mhm_id) : 1,
                    'sec_title'     => $course->title . ' - ' . $param['title'],
                    'sec_teacher'   => app(Account::class)->getUserNickName(),
                    'status'        => $param['status']
                ]
            ]);
        }

        return $create;
	}

	public function editHour($course_id, $sid, $param)
	{
        $data = app(CourseSection::class)->with(['course_base_info'])->findOrFail($sid);
        if ($course_id != $data->course_id) {
            throw new LogicException("课程与课时不匹配");
        }

        // 参数
        $data->status           = app(CourseSection::class)->getHourAuditStatus(request()->mid);
        $data->title            = $param['title'] 		?? $data->title;
        // $data->material_id      = $param['material_id'] ?? $data->material_id;
        $data->update_teacher   = request()->mid;
        $data->update_admin     = 0;

        $sec_data 			 = $this->findOrFail($data->course_data_id);
        $sec_data->attach_id = $param['course_data_attach_id'] 	?? $sec_data->attach_id;
        $sec_data->data_type = $param['course_data_type'] 		?? $sec_data->data_type;
        $sec_data->attach_id = $sec_data->data_type == 3 ? 0 : ($param['course_data_attach_id'] ?? $sec_data->attach_id);
        $sec_data->data_txt  = $sec_data->data_type == 3 ? ($param['course_data_txt'] ?? $sec_data->data_txt) : '';

        Db::startTrans();
        if ( !$data->save() || !$sec_data->save() ) {
            throw new LogicException("编辑失败");
            Db::rollback();
        }
        Db::commit();
        
        if ( $data->status != 1 ) { // 2机构审，3平台审
            Event::subscribe(ExamineNotify::class);
            Event::trigger('ExamineNotify', [
                'type'  => 'submit',   // 前端讲师提交，examine后台审核结果通知
                'data'  => [
                    'admin_user_id' => $data->status == 2 ? app(School::class)->getSchoolAdminUserId($data->getAttr('mhm_id')) : 1,
                    'sec_title'     => $data->course_title . ' - ' . $param['title'],
                    'sec_teacher'   => app(Account::class)->getUserNickName(),
                    'status'        => $data->status
                ]
            ]);
        }
        return $data;
	}
}
